package com.lanou.controller;

import com.lanou.bean.AdminInfo;
import com.lanou.bean.AdminModule;
import com.lanou.bean.RoleInfo;
import com.lanou.service.AdminInfoService;
import com.lanou.service.AdminRoleService;
import com.lanou.service.RoleInfoService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/14.
 */
@Controller
public class AdminRoleController {
    @Resource
    private RoleInfoService roleInfoService;
    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private AdminRoleService adminRoleService;

    /**
     * 根据管理员ID删除管理员信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRoleByAdminId")
    public AjaxResult deleteRole(@RequestParam("id") int id) {
        adminInfoService.delectAdmin(id);
        adminRoleService.deleteRoleByAdminId(id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/admin_list");
        return ajaxResult;
    }

    /**
     * 查询所有角色信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllRoleShow")
    public List<RoleInfo> findAllRoleShow(){
        System.out.println(roleInfoService.findAllRoles());
        return roleInfoService.findAllRoles();
    }

    /**
     * 添加管理员信息和对应角色
     * @param name
     * @param adminCode
     * @param password
     * @param tele
     * @param email
     * @param modules
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertAdmin")
    public AjaxResult insertAdminRole(@RequestParam(value = "name") String name,
                                      @RequestParam(value = "admincode") String adminCode,
                                      @RequestParam(value = "password") String password,
                                      @RequestParam(value = "tele") String tele,
                                      @RequestParam(value = "email") String email,
                                      @RequestParam(value = "modules[]") List<String> modules) {

        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setName(name);
        adminInfo.setAdminCode(adminCode);
        adminInfo.setPassword(password);
        adminInfo.setTelephone(tele);
        adminInfo.setEmail(email);
        adminInfo.setEnrolldate(new Date(System.currentTimeMillis()));
        adminInfoService.insertAdmin(adminInfo);
        int admin_id = adminInfo.getAdminId();
        List<String> modulesList = modules;
        for (String m : modulesList) {
            RoleInfo roleInfo = roleInfoService.selectRoleByRoleName(m);
            adminRoleService.insertAdminRole(admin_id,roleInfo.getRoleId());
        }
        AjaxResult result = new AjaxResult();
        result.setMsg("/admin_list");
        return result;
    }

    /**
     * 查询所有的管理员信息和对应的角色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllAdmin")
    public AdminModule selectAllAdmin(){
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        return adminInfoService.selectAllAdmin(adminInfo.getAdminId());
    }
}
